package br.com.iswe.gwt.ui.client;


import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.Widget;

public class LookUpPopUp extends ModalDialogBox {
	
	protected Widget content = null;
	protected String title;
	//protected Button btoFechar = null;
	protected String labelPropertyName = null;
	
	public LookUpPopUp(){
		//when calling this constructor 
		//the methods setContent and setTitle followed by the method setup must be called
		super(false);
	}
	
	public LookUpPopUp(Widget conteudo, String titulo) {
		super(false);
		this.content = conteudo;
		this.title = titulo;
		setup();
	}

	public void setup() {
		//setup the Dialog Box
		setupDialogBox(title);
		
		//Layout
		DockPanel deck = new DockPanel();
		//adiciona no popup
		setWidget(deck);
		
		/*
		if(title != null){
			//Barra Titulo
			Widget titleBar = buildTitleBar();
			deck.add(titleBar, DockPanel.NORTH);
		}
		*/
		
		if(content != null){
			//Conteudo
			deck.add(this.content, DockPanel.CENTER);
		}
		
		//Botao Fechar
		/*
		btoFechar = new Button("Close");
		btoFechar.addClickListener(new ClickListener(){public void onClick(Widget sender) {
			hide();
		}});
		
		deck.add(btoFechar, DockPanel.SOUTH);
		*/
		
		//Style
		setStyleName("iswe-lookup");
		//setStyleName("iswe-panelContents");
		//hide by default
		hide();	
	}

	protected Listener<String> searchListener = null;
	
	protected KeyUpHandler keyboardListener = new KeyUpHandler(){
		public void onKeyUp(KeyUpEvent event) {
			
			//if ESC is pressed
			if(KeyCodes.KEY_ESCAPE == event.getNativeKeyCode()){
				//close the popup without any further action
				hide();
			}
		}
	};

/*
	protected void setupSearchListener() {
		if(content instanceof BaseConsultarPanel && labelPropertyName != null){
			this.searchListener = new Listener(){
				public void execute(Object parametro) {
					BaseConsultarPanel consultarPanel = (BaseConsultarPanel)content;
					//Quando abrir o lookup ja com uma string de pesquisa, pesquisar pelo Label.
					consultarPanel.getFiltroPanel().getForm().setProperty(labelPropertyName, (String)parametro);
					consultarPanel.consultar();
				}
			};
		}
	}
	*/
	
	/*
	public void addFecharListener(ClickListener fecharListener) {
		btoFechar.addClickListener( fecharListener );
	}
*/
	public void show() {
		super.show();
		ClientUtils.centerPopUpHorizontal(this);
	}
	
	public void show(String searchString) {
		super.show();
		ClientUtils.centerPopUpHorizontal(this);
		if(searchListener != null && ClientUtils.isNotEmpty(searchString)){
			searchListener.execute(searchString);
		}
	}

	public void setContent(Widget content) {
		this.content = content;
	}

	public void setTitle(String titulo) {
		this.title = titulo;
	}

	
}
